<?php
/**
 * Twenty Fifteen functions and definitions
 *
 * Set up the theme and provides some helper functions, which are used in the
 * theme as custom template tags. Others are attached to action and filter
 * hooks in WordPress to change core functionality.
 *
 * When using a child theme you can override certain functions (those wrapped
 * in a function_exists() call) by defining them first in your child theme's
 * functions.php file. The child theme's functions.php file is included before
 * the parent theme's file, so the child theme functions would be used.
 *
 * @link https://codex.wordpress.org/Theme_Development
 * @link https://codex.wordpress.org/Child_Themes
 *
 * Functions that are not pluggable (not wrapped in function_exists()) are
 * instead attached to a filter or action hook.
 *
 * For more information on hooks, actions, and filters,
 * {@link https://codex.wordpress.org/Plugin_API}
 *
 * @package WordPress
 * @subpackage Twenty_Fifteen
 * @since Twenty Fifteen 1.0
 */

/**
 * Set the content width based on the theme's design and stylesheet.
 *
 * @since Twenty Fifteen 1.0
 */
if ( ! isset( $content_width ) ) {
	$content_width = 660;
}

/**
 * Twenty Fifteen only works in WordPress 4.1 or later.
 */
if ( version_compare( $GLOBALS['wp_version'], '4.1-alpha', '<' ) ) {
	require get_template_directory() . '/inc/back-compat.php';
}

if ( ! function_exists( 'twentyfifteen_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 *
 * @since Twenty Fifteen 1.0
 */
function twentyfifteen_setup() {

	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on twentyfifteen, use a find and replace
	 * to change 'twentyfifteen' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'twentyfifteen', get_template_directory() . '/languages' );

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 900, 506, true );
	add_image_size( 'news-thumbnails', 900, 506, true ); // (cropped)


	// This theme uses wp_nav_menu() in two locations.
	register_nav_menus( array(
		'primary' => __( 'Primary Menu',      'twentyfifteen' ),
		'social'  => __( 'Social Links Menu', 'twentyfifteen' ),
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
	) );

	/*
	 * Enable support for Post Formats.
	 *
	 * See: https://codex.wordpress.org/Post_Formats
	 */
	add_theme_support( 'post-formats', array(
		'aside', 'image', 'video', 'quote', 'link', 'gallery', 'status', 'audio', 'chat'
	) );

	$color_scheme  = twentyfifteen_get_color_scheme();
	$default_color = trim( $color_scheme[0], '#' );

	// Setup the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'twentyfifteen_custom_background_args', array(
		'default-color'      => $default_color,
		'default-attachment' => 'fixed',
	) ) );

	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style( array( 'css/editor-style.css', 'genericons/genericons.css', twentyfifteen_fonts_url() ) );
}
endif; // twentyfifteen_setup
add_action( 'after_setup_theme', 'twentyfifteen_setup' );

/**
 * Register widget area.
 *
 * @since Twenty Fifteen 1.0
 *
 * @link https://codex.wordpress.org/Function_Reference/register_sidebar
 */
function twentyfifteen_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Widget Area', 'twentyfifteen' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentyfifteen' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'twentyfifteen_widgets_init' );

if ( ! function_exists( 'twentyfifteen_fonts_url' ) ) :
/**
 * Register Google fonts for Twenty Fifteen.
 *
 * @since Twenty Fifteen 1.0
 *
 * @return string Google fonts URL for the theme.
 */
function twentyfifteen_fonts_url() {
	$fonts_url = '';
	$fonts     = array();
	$subsets   = 'latin,latin-ext';

	/* translators: If there are characters in your language that are not supported by Noto Sans, translate this to 'off'. Do not translate into your own language. */
	if ( 'off' !== _x( 'on', 'Noto Sans font: on or off', 'twentyfifteen' ) ) {
		$fonts[] = 'Noto Sans:400italic,700italic,400,700';
	}

	/* translators: If there are characters in your language that are not supported by Noto Serif, translate this to 'off'. Do not translate into your own language. */
	if ( 'off' !== _x( 'on', 'Noto Serif font: on or off', 'twentyfifteen' ) ) {
		$fonts[] = 'Noto Serif:400italic,700italic,400,700';
	}

	/* translators: If there are characters in your language that are not supported by Inconsolata, translate this to 'off'. Do not translate into your own language. */
	if ( 'off' !== _x( 'on', 'Inconsolata font: on or off', 'twentyfifteen' ) ) {
		$fonts[] = 'Inconsolata:400,700';
	}

	/* translators: To add an additional character subset specific to your language, translate this to 'greek', 'cyrillic', 'devanagari' or 'vietnamese'. Do not translate into your own language. */
	$subset = _x( 'no-subset', 'Add new subset (greek, cyrillic, devanagari, vietnamese)', 'twentyfifteen' );

	if ( 'cyrillic' == $subset ) {
		$subsets .= ',cyrillic,cyrillic-ext';
	} elseif ( 'greek' == $subset ) {
		$subsets .= ',greek,greek-ext';
	} elseif ( 'devanagari' == $subset ) {
		$subsets .= ',devanagari';
	} elseif ( 'vietnamese' == $subset ) {
		$subsets .= ',vietnamese';
	}

	if ( $fonts ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', $fonts ) ),
			'subset' => urlencode( $subsets ),
		), '//fonts.googleapis.com/css' );
	}

	return $fonts_url;
}
endif;

/**
 * Enqueue scripts and styles.
 *
 * @since Twenty Fifteen 1.0
 */
function twentyfifteen_scripts() {
	// Add custom fonts, used in the main stylesheet.
	wp_enqueue_style( 'twentyfifteen-fonts', twentyfifteen_fonts_url(), array(), null );

	// Add Genericons, used in the main stylesheet.
	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );

	// Load our main stylesheet.
	wp_enqueue_style( 'twentyfifteen-style', get_stylesheet_uri() );

	// Load the Internet Explorer specific stylesheet.
	wp_enqueue_style( 'twentyfifteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentyfifteen-style' ), '20141010' );
	wp_style_add_data( 'twentyfifteen-ie', 'conditional', 'lt IE 9' );

	// Load the Internet Explorer 7 specific stylesheet.
	wp_enqueue_style( 'twentyfifteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentyfifteen-style' ), '20141010' );
	wp_style_add_data( 'twentyfifteen-ie7', 'conditional', 'lt IE 8' );

	wp_enqueue_script( 'twentyfifteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20141010', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	if ( is_singular() && wp_attachment_is_image() ) {
		wp_enqueue_script( 'twentyfifteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20141010' );
	}

	wp_enqueue_script( 'twentyfifteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20141212', true );
	wp_localize_script( 'twentyfifteen-script', 'screenReaderText', array(
		'expand'   => '<span class="screen-reader-text">' . __( 'expand child menu', 'twentyfifteen' ) . '</span>',
		'collapse' => '<span class="screen-reader-text">' . __( 'collapse child menu', 'twentyfifteen' ) . '</span>',
	) );
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_scripts' );

/**
 * Add featured image as background image to post navigation elements.
 *
 * @since Twenty Fifteen 1.0
 *
 * @see wp_add_inline_style()
 */
function twentyfifteen_post_nav_background() {
	if ( ! is_single() ) {
		return;
	}

	$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
	$next     = get_adjacent_post( false, '', false );
	$css      = '';

	if ( is_attachment() && 'attachment' == $previous->post_type ) {
		return;
	}

	if ( $previous &&  has_post_thumbnail( $previous->ID ) ) {
		$prevthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'post-thumbnail' );
		$css .= '
			.post-navigation .nav-previous { background-image: url(' . esc_url( $prevthumb[0] ) . '); }
			.post-navigation .nav-previous .post-title, .post-navigation .nav-previous a:hover .post-title, .post-navigation .nav-previous .meta-nav { color: #fff; }
			.post-navigation .nav-previous a:before { background-color: rgba(0, 0, 0, 0.4); }
		';
	}

	if ( $next && has_post_thumbnail( $next->ID ) ) {
		$nextthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'post-thumbnail' );
		$css .= '
			.post-navigation .nav-next { background-image: url(' . esc_url( $nextthumb[0] ) . '); }
			.post-navigation .nav-next .post-title, .post-navigation .nav-next a:hover .post-title, .post-navigation .nav-next .meta-nav { color: #fff; }
			.post-navigation .nav-next a:before { background-color: rgba(0, 0, 0, 0.4); }
		';
	}

	wp_add_inline_style( 'twentyfifteen-style', $css );
}
add_action( 'wp_enqueue_scripts', 'twentyfifteen_post_nav_background' );

/**
 * Display descriptions in main navigation.
 *
 * @since Twenty Fifteen 1.0
 *
 * @param string  $item_output The menu item output.
 * @param WP_Post $item        Menu item object.
 * @param int     $depth       Depth of the menu.
 * @param array   $args        wp_nav_menu() arguments.
 * @return string Menu item with possible description.
 */
function twentyfifteen_nav_description( $item_output, $item, $depth, $args ) {
	if ( 'primary' == $args->theme_location && $item->description ) {
		$item_output = str_replace( $args->link_after . '</a>', '<div class="menu-item-description">' . $item->description . '</div>' . $args->link_after . '</a>', $item_output );
	}

	return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'twentyfifteen_nav_description', 10, 4 );

/**
 * Add a `screen-reader-text` class to the search form's submit button.
 *
 * @since Twenty Fifteen 1.0
 *
 * @param string $html Search form HTML.
 * @return string Modified search form HTML.
 */
function twentyfifteen_search_form_modify( $html ) {
	return str_replace( 'class="search-submit"', 'class="search-submit screen-reader-text"', $html );
}
add_filter( 'get_search_form', 'twentyfifteen_search_form_modify' );

/**
 * Implement the Custom Header feature.
 *
 * @since Twenty Fifteen 1.0
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 *
 * @since Twenty Fifteen 1.0
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Customizer additions.
 *
 * @since Twenty Fifteen 1.0
 */
require get_template_directory() . '/inc/customizer.php';


	
/* ------------------------------------------------------------------------ */
/* 後台自訂 STAR
/* ------------------------------------------------------------------------ */

// = 將右下角版本隱藏 =
function change_footer_version() {
  return '2016.08 Final | 版本 4.6';
}
add_filter( 'update_footer', 'change_footer_version', 9999 );

// = 將左下角文字隱藏 = 
function remove_footer_admin () {
  return '李明 | 0937 900 430 | work@leemingle.com';
}
add_filter('admin_footer_text', 'remove_footer_admin');

// = 新增物件在上面的bar = 

function wp_admin_bar_new_item2() {
global $wp_admin_bar;
$wp_admin_bar->add_menu(array(
'id' => 'wp-admin-bar-new-item2',
'title' => __('leemingle.com'),
'href' => 'http://leemingle.com'
));
}
add_action('wp_before_admin_bar_render', 'wp_admin_bar_new_item2');

// = 隱藏右上角help、說明選項、左上角外掛更新提醒 = 
function hide_help() {
    echo '<style type="text/css">
            #contextual-help-link-wrap { display: none !important; }
          </style>';
}
add_action('admin_head', 'hide_help');

// = 上方BAR = 
function wps_admin_bar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('wp-logo');
	$wp_admin_bar->remove_menu('comments');
	$wp_admin_bar->remove_menu('user-info');
    $wp_admin_bar->remove_menu('view-site');
    $wp_admin_bar->remove_menu('edit-profile');
	$wp_admin_bar->remove_menu('new-content');
	$wp_admin_bar->remove_menu('updates');
}
add_action( 'wp_before_admin_bar_render', 'wps_admin_bar' );

//修改後台登入畫面css
function custom_login_css() {
echo '<link rel="stylesheet" type="text/css" href="'.get_stylesheet_directory_uri().'/css/login-styles.css" />';
}
add_action('login_head', 'custom_login_css');

//修改後台Admin畫面css
function custom_admin_style() {
	echo '<link rel="stylesheet" type="text/css" href="'.get_stylesheet_directory_uri().'/css/admin-styles.css" />';
}

add_action('admin_head', 'custom_admin_style');
add_filter( 'the_archive_title', function ($title) {

    if ( is_category() ) {

            $title = single_cat_title( '', false );

        } elseif ( is_tag() ) {

            $title = single_tag_title( '', false );

        } elseif ( is_author() ) {

            $title = '<span class="vcard">' . get_the_author() . '</span>' ;

        }

    return $title;

});

//修改後台選單名稱
function edit_admin_menus() {
    global $menu;
    global $submenu;
     
    $menu[5][0] = '最新消息'; // Change Posts to Recipes
    $submenu['edit.php'][5][0] = '所有消息';
    $submenu['edit.php'][10][0] = '新增消息';
    $menu[20][0] = 'About-關於心元'; // Change Posts to Recipes
    remove_submenu_page('edit.php?post_type=page','post-new.php?post_type=page'); 
}
add_action( 'admin_menu', 'edit_admin_menus' );

//後台Body增加角色專用class
add_filter('admin_body_class', function($classes) {
    global $current_user;
 
    if(is_array($current_user->roles)) {
        foreach($current_user->roles as $role) {
            $classes .= "user-role-{$role} ";
        }
    }
 
    return rtrim($classes);
});

/* 後台自定新增選單-基本資料 */

$themename = "顯示設定";
$shortname = "nt";

$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
       $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category"); 

$options = array (
 
array( "name" => $themename." Options",
	"type" => "title"),
 

array( "name" => "顯示設定",
	"type" => "section"),
array( "type" => "open"),
 
array( "name" => "About-Section4",
	"desc" => "",
	"id" => $shortname."_about_s4",
	"type" => "select",
	"options" => array("open", "close"),
	"std" => "open"),
 
array( "type" => "close")
 
);


function mytheme_add_admin() {
 
global $themename, $shortname, $options;
 
if ( $_GET['page'] == basename(__FILE__) ) {
 
	if ( 'save' == $_REQUEST['action'] ) {
 
		foreach ($options as $value) {
		update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
 
foreach ($options as $value) {
	if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
 
	header("Location: admin.php?page=functions.php&saved=true");
die;
 
} 
else if( 'reset' == $_REQUEST['action'] ) {
 
	foreach ($options as $value) {
		delete_option( $value['id'] ); }
 
	header("Location: admin.php?page=functions.php&reset=true");
die;
 
}
}
 
add_menu_page($themename, $themename, 'administrator', basename(__FILE__), 'mytheme_admin');
}

function mytheme_add_init() {

$file_dir=get_bloginfo('template_directory');
wp_enqueue_style("functions", $file_dir."/functions/functions.css", false, "1.0", "all");
wp_enqueue_script("rm_script", $file_dir."/functions/rm_script.js", false, "1.0");

}
function mytheme_admin() {
 
global $themename, $shortname, $options;
$i=0;
 
if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';
 
?>
<div class="wrap rm_wrap">
<h2><?php echo $themename; ?></h2>
 
<div class="rm_opts">
<form method="post">
<?php foreach ($options as $value) {
switch ( $value['type'] ) {
 
case "open":
?>
 
<?php break;
 
case "close":
?>
 
</div>
</div>
<br />

 
<?php break;
 
case "title":
?>

 
<?php break;
 
case 'text':
?>

<div class="rm_input rm_text">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
 	<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id'])  ); } else { echo $value['std']; } ?>" />
 <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 
 </div>
<?php
break;
 
case 'textarea':
?>

<div class="rm_input rm_textarea">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
 	<textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?></textarea>
 <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 
 </div>
  
<?php
break;
 
case 'select':
?>

<div class="rm_input rm_select">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
	
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
<?php foreach ($value['options'] as $option) { ?>
		<option <?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>
</select>

	<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
</div>
<?php
break;
 
case "checkbox":
?>

<div class="rm_input rm_checkbox">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
	
<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>
<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />


	<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 </div>
<?php break; 
case "section":

$i++;

?>

<div class="rm_section">
<div class="rm_title"><h2><?php echo $value['name']; ?></h2>
</span><div class="clearfix"></div></div>
<div class="rm_options">

 
<?php break;
 
}
}
?>
 
<input type="hidden" name="action" value="save" />
<span class="submit"><input name="save<?php echo $i; ?>" type="submit" value="儲存" />
</form>
<form method="post"></form>
 </div> 
 

<?php
}
?>
<?php
add_action('admin_init', 'mytheme_add_init');
add_action('admin_menu', 'mytheme_add_admin');


/* 後台自定新增選單-基本資料 */
add_action('admin_menu', 'my_plugin_menu2');

function my_plugin_menu2() {
  add_menu_page('Index Options2', '關於心元標題', 3,'Index-Options2', 'my_plugin_options2');
}

function my_plugin_options2() {
    if ( $_POST['update_options'] == 'true' ) { options_update2(); }  //check options update
	?>
    <div class="wrap custom">
			<form method="post" action="">
        		<div class="col-6">
					<h2>里程碑</h2>
					<input type="hidden" name="update_options" value="true" />
		            <table class="form-table">
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_1en_url"><?php _e('英文'); ?></label></th>
		                    <td>
		                    	<textarea name="about_1en_url" id="about_1en_url"rows="1"><?php echo get_option('paragrams_about_1en_url'); ?></textarea>
		                    </td>
		                </tr>
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_1tw_url"><?php _e('繁體'); ?></label></th>
		                    <td>
		                    	<textarea name="about_1tw_url" id="about_1tw_url"rows="1"><?php echo get_option('paragrams_about_1tw_url'); ?></textarea>
		                    </td>
		                </tr>
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_1cn_url"><?php _e('簡體'); ?></label></th>
		                    <td>
		                    	<textarea name="about_1cn_url" id="about_1cn_url"rows="1"><?php echo get_option('paragrams_about_1cn_url'); ?></textarea>
		                    </td>
		                </tr>
		            </table>
        		</div>
        		<div class="col-6">
					<h2>團隊</h2>
					<input type="hidden" name="update_options" value="true" />
		            <table class="form-table">
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_2en_url"><?php _e('英文'); ?></label></th>
		                    <td>
		                    	<textarea name="about_2en_url" id="about_2en_url"rows="1"><?php echo get_option('paragrams_about_2en_url'); ?></textarea>
		                    </td>
		                </tr>
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_2tw_url"><?php _e('繁體'); ?></label></th>
		                    <td>
		                    	<textarea name="about_2tw_url" id="about_2tw_url"rows="1"><?php echo get_option('paragrams_about_2tw_url'); ?></textarea>
		                    </td>
		                </tr>
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_2cn_url"><?php _e('簡體'); ?></label></th>
		                    <td>
		                    	<textarea name="about_2cn_url" id="about_2cn_url"rows="1"><?php echo get_option('paragrams_about_2cn_url'); ?></textarea>
		                    </td>
		                </tr>
		            </table>
        		</div>
        		<div class="col-6">
					<h2>顧問</h2>
					<input type="hidden" name="update_options" value="true" />
		            <table class="form-table">
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_3en_url"><?php _e('英文'); ?></label></th>
		                    <td>
		                    	<textarea name="about_3en_url" id="about_3en_url"rows="1"><?php echo get_option('paragrams_about_3en_url'); ?></textarea>
		                    </td>
		                </tr>
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_3tw_url"><?php _e('繁體'); ?></label></th>
		                    <td>
		                    	<textarea name="about_3tw_url" id="about_3tw_url"rows="1"><?php echo get_option('paragrams_about_3tw_url'); ?></textarea>
		                    </td>
		                </tr>
		                <tr valign="top">
		                    <th scope="row" style="width:120px;"><label for="about_3cn_url"><?php _e('簡體'); ?></label></th>
		                    <td>
		                    	<textarea name="about_3cn_url" id="about_3cn_url"rows="1"><?php echo get_option('paragrams_about_3cn_url'); ?></textarea>
		                    </td>
		                </tr>
		            </table>
        		</div>
        		<br class="clear">
				<p><input type="submit" value="Save" class="button button-primary" /></p>
			</form>
    </div>

<?php
}
// Update options
function options_update2() {
	update_option('paragrams_about_1en_url', 	$_POST['about_1en_url']);
	update_option('paragrams_about_1tw_url', 	$_POST['about_1tw_url']);
	update_option('paragrams_about_1cn_url', 	$_POST['about_1cn_url']);
	update_option('paragrams_about_2en_url', 	$_POST['about_2en_url']);
	update_option('paragrams_about_2tw_url', 	$_POST['about_2tw_url']);
	update_option('paragrams_about_2cn_url', 	$_POST['about_2cn_url']);
	update_option('paragrams_about_3en_url', 	$_POST['about_3en_url']);
	update_option('paragrams_about_3tw_url', 	$_POST['about_3tw_url']);
	update_option('paragrams_about_3cn_url', 	$_POST['about_3cn_url']);
}
/**
 * Register a custom menu page.
 */
function wpdocs_register_my_custom_menu_page() {
    add_menu_page(
        __( 'Custom Menu Title', 'textdomain' ),
        '熱門手動排序',
        'manage_options',
        'options-general.php?page=q2w3-post-order&p_type=type_companies',
        '',
        plugins_url( 'image.svg' ),
        5
    );
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );
/* 後台自定新增選單-基本資料 */
add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
  add_menu_page('Index Options', '社群/首頁', 0, 'Index-Options', 'my_plugin_options');
}

function my_plugin_options() {
    if ( $_POST['update_options'] == 'true' ) { options_update(); }  //check options update
	?>
    <div class="wrap custom">
			<form method="post" action="">
        		<div class="col-6">
				<h2>社群連結</h2>
				<input type="hidden" name="update_options" value="true" />
	            <table class="form-table">
	                <tr valign="top">
	                    <th scope="row" style="width:120px;"><label for="contact_fb_url"><?php _e('Facebook'); ?></label></th>
	                      <td>
	                        <textarea name="contact_fb_url" id="contact_fb_url"rows="1"><?php echo get_option('paragrams_contact_fb_url'); ?></textarea>
	                       </td>
	                </tr>
	                <tr valign="top">
	                    <th scope="row" style="width:120px;"><label for="contact_we_url"><?php _e('WeChat'); ?></label></th>
	                      <td>
	                        <textarea name="contact_we_url" id="contact_we_url"rows="1"><?php echo get_option('paragrams_contact_we_url'); ?></textarea>
	                       </td>
	                </tr>
	                <tr valign="top">
	                    <th scope="row" style="width:120px;"><label for="contact_tw_url"><?php _e('Twitter'); ?></label></th>
	                      <td>
	                        <textarea name="contact_tw_url" id="contact_tw_url"rows="1"><?php echo get_option('paragrams_contact_tw_url'); ?></textarea>
	                       </td>
	                </tr>
	                <tr valign="top">
	                    <th scope="row" style="width:120px;"><label for="contact_md_url">Medium</label></th>
	                      <td>
	                        <textarea name="contact_md_url" id="contact_md_url"rows="1"><?php echo get_option('paragrams_contact_md_url'); ?></textarea>
	                      </td>
	                </tr>
	            </table>
	        </div>
	        <div class="col-6">
				<h2>首頁設定</h2>
	            <table class="form-table">
	                <tr>
	                    <th scope="row" style="width:120px;"><label for="index_title_tw">標題-繁體</label></th>
	                    <td>
	                    	<textarea name="index_title_tw" id="index_title_tw"rows="2"><?php echo get_option('paragrams_index_title_tw'); ?></textarea>
	                    </td>
	                </tr>
	                <tr>
	                    <th scope="row" style="width:120px;"><label for="index_title_cn">標題-簡體</label></th>
	                    <td>
	                    	<textarea name="index_title_cn" id="index_title_cn"rows="2"><?php echo get_option('paragrams_index_title_cn'); ?></textarea>
	                    </td>
	                </tr>
	                <tr>
	                    <th scope="row" style="width:120px;"><label for="index_title_en">標題-英文</label></th>
	                    <td>
	                    	<textarea name="index_title_en" id="index_title_en"rows="2"><?php echo get_option('paragrams_index_title_en'); ?></textarea>
	                    </td>
	                </tr>
	                <tr>
	                    <th scope="row" style="width:120px;"><label for="index_total_title">統計-標題</label></th>
	                    <td>
	                    	<textarea name="index_total_title" id="index_total_title"rows="1"><?php echo get_option('paragrams_index_total_title'); ?></textarea>
	                    </td>
	                </tr>
	                <tr>
	                    <th scope="row" style="width:120px;"><label for="index_total_number">統計-數據</label></th>
	                    <td>
	                    	<textarea name="index_total_number" id="index_total_number"rows="1"><?php echo get_option('paragrams_index_total_number'); ?></textarea>
	                    </td>
	                </tr>
	                <tr>
	                    <th scope="row" style="width:120px;"><label for="index_total_number_unit">統計-數據單位</label></th>
	                    <td>
	                    	<textarea name="index_total_number_unit" id="index_total_number_unit"rows="1"><?php echo get_option('paragrams_index_total_number_unit'); ?></textarea>
	                    </td>
	                </tr>
	            </table>
        	</div>
        	<br class="clear">
			<p><input type="submit" value="Save" class="button button-primary" /></p>
			</form>
    </div>

<?php
}
// Update options
function options_update() {
	update_option('paragrams_contact_fb_url', 	$_POST['contact_fb_url']);
	update_option('paragrams_contact_we_url', 	$_POST['contact_we_url']);
	update_option('paragrams_contact_tw_url', 	$_POST['contact_tw_url']);
	update_option('paragrams_contact_md_url', 	$_POST['contact_md_url']);
	update_option('paragrams_index_title_tw', 	$_POST['index_title_tw']);
	update_option('paragrams_index_title_cn', 	$_POST['index_title_cn']);
	update_option('paragrams_index_title_en', 	$_POST['index_title_en']);
	update_option('paragrams_index_total_title', 	$_POST['index_total_title']);
	update_option('paragrams_index_total_number', 	$_POST['index_total_number']);
	update_option('paragrams_index_total_number_unit', 	$_POST['index_total_number_unit']);
}

// 顯示該年份Archive年份
function pc_theme_get_archives_link ( $link_html ) {
    global $wp;
    static $current_url;
    if ( empty( $current_url ) ) {
        $current_url = add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
    }
    if ( stristr( $link_html, $current_url ) !== false ) {
        $link_html = preg_replace( '/(<[^\s>]+)/', '\1 selected', $link_html, 1 );
    }
    return $link_html;
}
add_filter('get_archives_link', 'pc_theme_get_archives_link');

 // Admin 選單順序
  function custom_menu_order($menu_ord) {
    if (!$menu_ord) return true;
    return array(
      'index.php', // Dashboard
      'edit.php?post_type=type_slideshow', // Custom type one
      'edit.php?post_type=page', // Pages
      'edit.php?post_type=type_chart', // Custom type one
      'edit.php?post_type=type_milestone', // Custom type five
      'edit.php?post_type=type_team', // Custom type five
      'edit.php?post_type=type_partner', // Custom type five
      'edit.php', // Posts
      'edit.php?post_type=type_companies', // Custom type five
      'edit.php?post_type=type_slideshow_com', // Custom type one
      'edit.php?post_type=type_jobs', // Custom type five
      'edit.php?post_type=type_contact', // Custom type five
      'separator1', // First separator
      'upload.php', // Media
      'link-manager.php', // Links
      'separator2', // Second separator
      'themes.php', // Appearance
      'plugins.php', // Plugins
      'users.php', // Users
      'tools.php', // Tools
      'options-general.php', // Settings
      'separator-last', // Last separator
    );
  }

  add_filter('custom_menu_order', 'custom_menu_order'); // Activate custom_menu_order
  add_filter('menu_order', 'custom_menu_order');

  
//後台Body增加角色專用class
add_filter('admin_body_class', function($classes) {
    global $current_user;
 
    if(is_array($current_user->roles)) {
        foreach($current_user->roles as $role) {
            $classes .= "user-role-{$role} ";
        }
    }
 
    return rtrim($classes);
});

function asset($path){
	echo bloginfo('template_directory').'/'.$path;
}

function langEcho($string){
//[:tw]英语流利说获近亿美元C轮融资，已实现规模化盈利[:zh]Liulishuo[:en]Liulishuo[:]
	$preg = '/\[\:'.qtrans_getLanguage().'\](.*?)\[\:/is';
	preg_match($preg, $string, $mat);
	if (!$mat[1]) echo $string;
	else echo $mat[1];
}

function the_excerpt_without_p() {
	$html = apply_filters( 'the_excerpt', get_the_excerpt() );
	$html = preg_replace('/<.*?>/is', '', $html);
	echo $html;
}

function the_pure_content( $more_link_text = null, $strip_teaser = false) {
	$content = get_the_content( $more_link_text, $strip_teaser );

	/**
	 * Filters the post content.
	 *
	 * @since 0.71
	 *
	 * @param string $content Content of the current post.
	 */
	$content = apply_filters( 'the_content', $content );
	$content = str_replace( ']]>', ']]&gt;', $content );
    $content = preg_replace('/<.*?>/is', '', $content);
	echo $content;
}

function the_post_thumbnail_src( $size = 'post-thumbnail', $attr = '' ) {
	$img = get_the_post_thumbnail( null, $size, $attr );
    preg_match('/src=\"(.*?)\"/is', $img, $mat);
    echo $mat[1];
}

/**
 * WordPress 添加额外选项字段到常规设置页面
 */
$new_general_setting = new new_general_setting();
class new_general_setting {
	function new_general_setting( ) {
		add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
	}
	function register_fields() {
		register_setting( 'general', 'recent_setting', 'esc_attr' );
		add_settings_field('recent_setting', '<label for="recent_setting">'.__('近期文章時間' ).'</label>' , array(&$this, 'fields_html') , 'general' );
	}
	function fields_html() {
		$value = get_option( 'recent_setting', '' );
		echo '<input type="text" id="recent_setting" name="recent_setting" value="' . $value . '" />天內';
	}
}